const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    password: "",
    code: null
  },

  bindUsername: function(e) {
    this.setData({
      username: e.detail.value
    });
  },

  bindPassword: function(e) {
    this.setData({
      password: e.detail.value
    });
  },
  onFocusPsd: function() {
    this.setData({
      psdFocus: 'psdFocus'
    })
  },
  onBlurPsd: function() {
    this.setData({
      psdFocus: ''
    })
  },
  onFocusName: function() {
    this.setData({
      nameFocus: 'nameFocus'
    })
  },
  onBlurName: function() {
    this.setData({
      nameFocus: ''
    })
  },
  index: function() {
    app.setUrlBool();
  },
  /**
   * 微信账号与系统账号进行绑定
   */
  Accountbind: function(data, callback) {
    var _this = this;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=WeixinApp&act=bindAcount&token=' + wx.getStorageSync('token'),
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) { //开发者服务返回的回调函数

        if (typeof callback == 'function') {
          callback(res)
        }

      }
    })
  },
  /**
   * 系统账号登录
   */
  login: function() {
    var _this = this;
    var data = {};
    var userNameInput = _this.data.username;
    var passwordInput = _this.data.password;
    data.username = userNameInput;
    data.password = passwordInput;
    if (userNameInput == "") {
      wx.showToast({
        title: '请输入用户名！',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (passwordInput == "") {
      wx.showToast({
        title: '请输入密码！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    _this.Accountbind(data, function(res) {
      _this.index();
      if (res.data.error_code == 0) {
        try {
          //wx.clearStorageSync();
          wx.setStorageSync('bindAcountBool', true); console.warn(new Date(), 'bind login - token', wx.getStorageSync('token'));
          wx.setStorageSync('token', res.data.data.token); //更改token
          wx.setStorageSync('userId', res.data.data.user_info.uid);//修改userId
          app.setUrlBool();
          if (_this.data.code) {
            wx.reLaunch({
              url: "/pages/testImg/testImg?scene=" + escape(JSON.stringify(_this.data.code))
            });
          } else {
            wx.reLaunch({
              url: "/pages/index/index"
            });
          }

        } catch (e) {

        }
      } else {
        wx.showToast({
          title: res.data.error_msg,
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this,
        code = options.code;
    if (code) {
      code = JSON.parse(unescape(code));
      _this.setData({
        code: code
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})